<?php
/**
 * 辅助控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Common extends Controller
{
	private $result;
	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('common_model');
		$this->loadModel('ads_user_model');
		$this->loadClass('Mcache');
	}
	/**
	 * 默认执行方法
	 *
	 * @return
	 */
	function index()
	{

	}

	/**
	 * 本方法为ajax方法——根据provice的code码来获取对应的城市或者区
	 *
	 * @return ajax json
	 */
	function getCitysByPcode()
	{

		$code = intval($this->in_var['pcode']);

		echo json_encode(getCitysByPid($code));
	}

	/**
	 * 根据条件获取城市列表
	 *
	 * @return ajax json
	 */
	function getCitys()
	{
		$pid  = intval($this->in_var['pid']);
		$name = htmlspecialchars($this->in_var['name']);

		$address = $this->Mcache->getCache('pay','address');

		if(empty($address[$pid]['city']))
		{
			$citys = $this->common_model->getCitys($pid);

			$address[$pid]['city'] = $citys;

			$this->Mcache->setCache('pay','address',$address);

		}

		if(!empty($name))
		{
			$citys = array();

			foreach($address[$pid]['city'] as $city)
			{
				if(strpos($city['name'],$name))
				{
					$citys[] = $city;
				}
			}

			echo json_encode($citys);
			exit;

		}

		echo json_encode($address[$pid]['city']);

	}

	/**
	 * 根据条件获取区县数据
	 *
	 * @return ajax json
	 */
	function getDistricts()
	{
		$pid  = intval($this->in_var['pid']);
		$cid  = intval($this->in_var['cid']);
		$name = htmlspecialchars($this->in_var['name']);

		$address = $this->Mcache->getCache('pay','address');

		if(empty($address[$pid][$cid]['district']))
		{

			$districts = $this->common_model->getDistricts($pid,$cid);

			$address[$pid][$cid]['district'] = $districts;

			$this->Mcache->setCache('pay','address',$address);

		}

		if(!empty($name))
		{
			$towns = array();

			foreach($address[$pid][$cid]['district'] as $town)
			{
				if(strpos($town['name'],$name))
				{
					$towns[] = $town;
				}
			}

			echo json_encode($towns);
			exit;

		}

		echo json_encode($address[$pid][$cid]['district']);

	}

	/**
	 * 根据条件获取学校数据
	 *
	 * @return ajax json
	 */
	function getSchools()
	{

		$pid  = intval($this->in_var['pid']);
		$cid  = intval($this->in_var['cid']);
		$did  = intval($this->in_var['did']);
		$name = htmlspecialchars($this->in_var['name']);

		$address = $this->Mcache->getCache('pay','address');

		if(empty($address[$pid][$cid][$did]))
		{
			$schools = $this->common_model->getSchools($pid,$cid,$did);

			$address[$pid][$cid][$did] = $schools;

			$this->Mcache->setCache('pay','address',$address);
		}

		if(!empty($name))
		{
			$sch = array();

			foreach($address[$pid][$cid][$did] as $school)
			{
				if(strpos($school['name'],$name))
				{
					$sch[] = $school;
				}
			}

			echo json_encode($sch);
			exit;

		}

		echo json_encode($address[$pid][$cid][$did]);

	}

	/**
	 * 验证码
	 * @return unknown_type
	 */
	function verify()
	{
		$args['verify_m'] = $_GET['verify_m'];
		$args['verify_a'] = $_GET['verify_a'];
		view::display( $this->tpl, $args );
	}

	/**
	 * 用户信息设定
	 *
	 */
	function initLoginData()
	{
		$this->tpl->assign('type', strtolower($this->in_var['type']));
		$this->tpl->assign('service', strtolower($this->in_var['service']));

		if($this->ads_user_model->isLogin())
		{
			$this->tpl->assign('hasLogin', true);
			$this->tpl->assign('uid', $_SESSION['pay']['login_info']['user_id']);
		}
	}

	/**
	 * 带签登录
	 */
	function loginSign()
	{
		$session = $this->in_var['session'];
		$game_id = $this->in_var['gameid'];
		$user_id = $this->in_var['userid'];
			
		if( !empty($session) && $game_id == '10000')
		{
			$this->result = $this->ads_user_model->loginSession($user_id,$game_id,$session);
		}
		else
		{
			$this->result = $this->ads_user_model->loginSign($this->in_var['sign'],$this->in_var['userid'],$this->in_var['time']);
		}

	}

	/**
	 * 获取带签
	 */
	function getSign()
	{
			
		$this->result = $this->ads_user_model->getSign('50438');
		$params = http_build_query($this->result);
		Goto(get_url('account','index').'?'.$params,'header','document',false);
	}

	/**
	 * 获取带签
	 */
	function getSession()
	{
		$ip = $this->in_var['IP_ADDRESS'];

		$this->result = $this->ads_user_model->getSession('50438','10000',$ip);

		Goto(get_url('account','index').'?gameid=10000&userid='.$this->result['userid'].'&session='.$this->result['session'],'header','document',false);
	}

	/**
	 * 验证码校验
	 * @return unknown_type
	 */
	function validateYzm(){
		
		$seccode  = trim(strtolower($this->in_var['seccode']));
		$sec      = trim(strtolower($_SESSION['login_check_number']));

		if(empty($seccode)||$seccode != $sec)
		{
			echo 0;
			exit;
		}
		else
		{
			echo 1;
		}
	}

}